package com.rc.evcharger.service.car;

import com.github.pagehelper.util.StringUtil;
import com.rc.evcharger.mapper.car.TenantCarNumberMapper;
import com.rc.evcharger.model.car.TenantCarNumber;
import com.rc.evcharger.model.car.TenantCarNumberExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by fenglibin on 2019/09/23.
 */
@Service
public class TenantCarNumberServiceImpl implements TenantCarNumberService {

    @Autowired
    private TenantCarNumberMapper tenantCarNumberMapper;

    @Override
    public TenantCarNumberMapper getMapper() {
        return tenantCarNumberMapper;
    }

    @Override
    public String findCarNumByMemberId(Long memberId, String tenantCode) {

        TenantCarNumberExample tenantCarNumberExample = new TenantCarNumberExample();

        //查询默认车牌号
        tenantCarNumberExample.createCriteria().andMemberIdEqualTo(memberId)
                .andTenantCodeEqualTo(tenantCode).andIsDefaultEqualTo("1");

        List<TenantCarNumber> tenantCarNumbers = tenantCarNumberMapper.selectByExample(tenantCarNumberExample);
        if (CollectionUtils.isEmpty(tenantCarNumbers)) {
            return null;
        }

        TenantCarNumber tenantCarNumber = tenantCarNumbers.get(0);

        return tenantCarNumber.getCarArea() + tenantCarNumber.getCarLetter() + tenantCarNumber.getCarNum();
    }


    @Override
    public TenantCarNumber selectByVinCode(String vinCode, String tenantCode) {
        TenantCarNumberExample tenantCarNumberExample = new TenantCarNumberExample();

        //查询默认车牌号
        //StartType启动方式 0 一般 1 刷卡 2 VIN码启动 默认0
        tenantCarNumberExample.createCriteria().andCarVinEqualTo(vinCode)
                .andTenantCodeEqualTo(tenantCode).andStartTypeEqualTo("2");

        List<TenantCarNumber> tenantCarNumbers = tenantCarNumberMapper.selectByExample(tenantCarNumberExample);
        if (CollectionUtils.isEmpty(tenantCarNumbers)) {
            return null;
        }

        TenantCarNumber tenantCarNumber = tenantCarNumbers.get(0);

        return tenantCarNumber;
    }


    @Override
    public String findCarNumberByVin(String vin, String tenantCode, Integer temp) {
        if (StringUtil.isEmpty(vin)) {
            return null;
        }
        boolean result = vin.matches("[a-zA-Z0-9]+");
        if (!result) {
            return null;
        }
        TenantCarNumber carNumber = this.selectByVinCode(vin, tenantCode);
        if (carNumber == null) {
            return null;
        }

        String carnum0 = "";
        if (carNumber.getCarArea() != null) {
            carnum0 += carNumber.getCarArea();
        }
        if (carNumber.getCarLetter() != null) {
            carnum0 += carNumber.getCarLetter();
        }
        if (carNumber.getCarNum() != null) {
            carnum0 += carNumber.getCarNum();
        }
        return carnum0;
    }
}
